package LeetCode;
//给定一个链表，返回链表开始入环的第一个节点。 如果链表无环，则返回 null
public class Num142 {
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                ListNode third = head;
                while (slow != third) {
                    third = third.next;
                    slow = slow.next;
                }
                return slow;
            }
        }
        return null;
    }
}
